package com.example.demo.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.demo.entity.Message;
import com.example.demo.util.EmailUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
public class SendCodeController {
    @RequestMapping("/needcode")
    public void Send(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        String email=request.getParameter("email");
        String code= EmailUtil.sendEmail("3121289785@qq.com",email);
        if(!"".equals(code)) {
            response.getWriter().write(JSONObject.toJSONString(Message.success("Success send email code")));
            request.getSession().setAttribute("emailCode",code);
        }else{
            response.getWriter().write(JSONObject.toJSONString(Message.failure(400,"Email code is send error")));
        }
    }
}
